import moment from 'moment/moment';

// 时间戳转时间
export const momentDate = (value, formatString) => {
  formatString = formatString || 'YYYY-MM-DD HH:mm:ss';
  return moment.unix(value).format(formatString);
};

//时间转时间戳
export const momentTurn = ((value) => {
	let timestamp = parseInt(new Date(value).getTime() / 1000);
	return timestamp;
})


/**
 * 字符串是否为空
 * 字符串类型判断非空且不为空串
 */
export const NullString = function(value) {
  return !value || value.toString().trim().length === 0;
};

/**
 * 是否为手机号-可能有国际号，取消号码限制
 * @param string
 * @return {boolean}
 * @constructor
 */
export const CheckPhone = function(val) {
  const reg = /^([1-9]\d*|[0]{1,1})$/;
  if (!reg.test(val)) {
    return false;
  }
  return true;
};

export const checkRunPhone = function(val) {
	const reg = /^1[23456789]\d{9}$/;
	if(!reg.test(val)){
		return false
	} else {
		return true
	}
}

/**
 * 对象是否为空
 * @param obj
 * @return {boolean}
 * @constructor
 */
export const NullObject = function(obj) {
  return !!Object.keys(obj).length;
};

/**
 * 数组是否为空
 * @param arr
 * @return {boolean}
 * @constructor
 */
export const NullArray = function(arr) {
  return !!arr.length;
};

export const isUndef = v => {
  return v === undefined || v === null;
};

/**
 * 防抖函数
 * handle { callback }
 * delay { Number }
 */
export const debounce = (handle, delay = 300) => {
  let timer = null;
  return function(...args) {
    const _this = this;
    clearTimeout(timer);
    timer = setTimeout(() => {
      handle.apply(_this, args);
    }, delay);
  };
};

/**
 * 节流
 * @param handle {Function}
 * @param wait {Number}
 * @return {function(...[*]=): void}
 */
export const throttle = (handle, wait = 300) => {
  let prev = new Date().getTime();
  return function(...args) {
    const ctx = this;
    const now = new Date().getTime();
    if (now - prev >= wait) {
      handle.apply(ctx, args);
      prev = now;
    }
  };
};

/**
 * 复制
 * @param content { String }
 * @param title { String }
 */
export const copyFunc = (content, title = '复制成功') => {
  uni.setClipboardData({
    data: content,
    success() {
      uni.hideToast();
      uni.showToast({
        title,
        icon: 'none',
      });
    },
  });
};

/**
 * 路径参数提取
 * @param url { String }
 * @return { Array }
 */
export const splitUrl = url => {
  // 拼接数组
  const arrUrl = url.split('&');
  const arrUrl1 = arrUrl.map(ele => ele.split('='));
  const arrUrl2 = arrUrl1.reduce(function(pre, cur) {
    pre[cur[0]] = decodeURIComponent(cur[1]) || '';
    return pre;
  }, {});

  return arrUrl2;
};

/**
 * 删除url中指定参数
 * @param names 数组或字符串
 * @returns {string}
 */
/* eslint-disable */
export const urlDel = (url, names) => {
  if (typeof names === 'string') {
    names = [names];
  }

  const obj = {};
  const arr = url.split('?')[1].split('&');
  // 获取参数转换为object
  for (var i = 0; i < arr.length; i++) {
    arr[i] = arr[i].split('=');
    obj[arr[i][0]] = arr[i][1];
  }
  // 删除指定参数
  for (var i = 0; i < names.length; i++) {
    delete obj[names[i]];
  }
  // 重新拼接url
  var url = `${url.split('?')[0]}?${JSON.stringify(obj)
    .replace(/[\"\{\}]/g, '')
    .replace(/\:/g, '=')
    .replace(/\,/g, '&')}`;
  return url;
};
/* eslint-disable */

/**
 * 路径参数拼接
 * @param url { String }
 * @param query { Object }
 * @return { String }
 */
export const concatShareQuery = (url, query = {}) => {
  let result = '';
  let beforeUrl = url;
  let afterUrl = '';
  const hasRouter = !!(beforeUrl.indexOf('#') > -1);
  const hasParams = !!(beforeUrl.indexOf('?') > -1);

  if (hasRouter) {
    beforeUrl = url.split('#')[0];
    afterUrl = url.split('#')[1];
  }

  result = hasParams ? '&' : '?';

  const isSplitUrl = splitUrl(beforeUrl.split('?')[1] || '');
  Object.keys(query).forEach((k, index) => {
    if (!isSplitUrl || !(isSplitUrl[k] || isSplitUrl[k] === '')) {
      const val = query[k];
      result += `${index > 0 ? '&' : ''}${k}=${val}`;
    }
  });

  return beforeUrl + result + (hasRouter ? `#${afterUrl}` : '');
};
